﻿<!DOCTYPE html>
<html>
	<head>
		<title>栏目树</title>
        $css()$js()
	</head>
	<body id="tree">
   
        <div class="tree" id="menu_tree">
            ${tree}
        </div>
        <style type="text/css">
            body{margin:0px;}
            #menu_tree{padding:10px 0 0 10px;}
            #menu_tree dt,#menu_tree dd{margin:0;padding:0;}
        </style>
        <script type="text/javascript">
            var treeFor = '${treefor}';
            function execCategory(cid, title) {
            	alert(cid);
                if (treeFor == 'archives') {
                    window.M.tab.show(title, '?module=archive&action=list&category.id=' + cid, '1_996_' + cid);
                }
            }

            var menuTree = cms.$('menu_tree');

            window.initCTree = function () {
                var menuSpan = menuTree.getElementsByClassName ? menuTree.getElementsByClassName('txt'):document.getElementsByClassName('txt', menuTree);
                var spanTxt;
                for (var i = 0; i < menuSpan.length; i++) {
                    spanTxt = menuSpan[i].innerHTML.replace(/<[^>]+>/ig,'');
                    menuSpan[i].innerHTML = '<a href="javascript:;">' + spanTxt + '</a>';
                    menuSpan[i].getElementsByTagName('A')[0].onclick = (function (cid, title) {
                        return function () {
                            execCategory(cid, title);
                        };
                    })(menuSpan[i].getAttribute('cid'), spanTxt);
                }


                var dts = menuTree.getElementsByTagName('img');
                cms.each(dts, function (i, e) {
                    //if (i == 0) return;     //标题除开
                    if (e.className.indexOf('tree-collage') == -1 && e.className.indexOf('tree-expand') == -1) return;

                    e.onclick = function (event) {

                        /*
                        //如果为链接，则不展开
                        var _event = window.event || event;
                        var sourceEle = _event.target || _event.srcElement;
                        if (sourceEle.nodeName == 'A') return;
                    

                        var lstImg = this.getElementsByTagName('IMG');
                        lstImg = lstImg[lstImg.length - 1];
                        var isExpand = lstImg.className.indexOf('tree-expand') != -1;
                        */

                        var isExpand = this.className.indexOf('tree-expand') != -1;

                        //折叠
                        if (isExpand) {
                            this.className = this.className.replace(/tree-expand/ig, 'tree-collage');
                            cms.each(this.parentNode.parentNode.getElementsByTagName('DD'), function (i1, e1) {
                                e1.style.display = 'none';
                            });
                        } else {
                            this.className = this.className.replace(/tree-collage/ig, 'tree-expand');
                            var dl = this.parentNode.parentNode;
                            var dds = dl.getElementsByTagName('DD');

                            //直接显示dd
                            if (dds.length != 0) {
                                cms.each(dds, function (i1, e1) {
                                    e1.style.display = '';
                                });
                            } else {
                                //从服务端加载内容
                                var _level = this.parentNode.getAttribute('level');
                                var _lft = this.parentNode.getAttribute('lft');
                                var _lines = (!this.parentNode.getElementsByClassName ? document.getElementsByClassName('tree-line', this.parentNode) : this.parentNode.getElementsByClassName('tree-line')).length;

                                cms.xhr.get('?module=category&action=gettree&level=' + _level + '&lft=' + _lft + '&lines=' + _lines, function (result) {
                                    dl.innerHTML += result;
                                    window.initCTree();
                                });
                            }
                        }
                    };
                });
            };


            window.initCTree();
        </script>

      
	</body> 
</html>